package com.atguigu.tingshu.order.template;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * title:
 * author: zgl
 * date: 2024/3/16
 * description：
 */
@OrderKinds(OrderType.TRACK_ORDER_TYPE)
public class TrackConfirm extends OrderConfirm{
    @Autowired
    public TrackConfirm(RedisTemplate redisTemplate){
        super(redisTemplate);
    }
    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Resource
    private TrackInfoFeignClient trackInfoFeignClient;
    @Override
    public void confirm(OrderInfoVo orderInfoVo,TradeVo tradeVo) {
            //购买数量的校验
            if (tradeVo.getTrackCount() <= 0) {
                throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
            }
            //根据声音id查询声音信息
            Result<TrackInfo> trackInfoResult = this.trackInfoFeignClient.getTrackInfo(tradeVo.getItemId());
            Assert.notNull(trackInfoResult, "声音订单确认页：获取声音信息失败！");
            TrackInfo trackInfo = trackInfoResult.getData();
            if (trackInfo == null) {
                throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
            }
            //根据trackInfo中的albumId查询专辑信息
            Result<AlbumInfo> albumInfoResult = this.albumInfoFeignClient.getAlbumInfo(trackInfo.getAlbumId());
            Assert.notNull(albumInfoResult, "声音订单确认页：获取专辑信息失败！");
            AlbumInfo albumInfo = albumInfoResult.getData();
            if (albumInfo == null) {
                throw new GuiguException(ResultCodeEnum.ILLEGAL_REQUEST);
            }
            //设置订单原始金额
            BigDecimal price = albumInfo.getPrice();
            BigDecimal originalAmount = price.multiply(new BigDecimal(tradeVo.getTrackCount()));
            orderInfoVo.setOriginalAmount(originalAmount);
            //设置订单总金额
            BigDecimal orderAmount = originalAmount;
            orderInfoVo.setOrderAmount(orderAmount);
            //设置订单减免总金额
            BigDecimal derateAmount = new BigDecimal("0.00");
            orderInfoVo.setDerateAmount(derateAmount);
            //查询本次购买的声音列表
            Result<List<TrackInfo>> buyTracksResult = this.trackInfoFeignClient.findBuyTracks(tradeVo.getItemId(), tradeVo.getTrackCount());
            Assert.notNull(buyTracksResult,"声音订单确认页：购买声音列表失败！");
            List<TrackInfo> buyTracks = buyTracksResult.getData();
            Assert.notNull(buyTracks,"声音订单确认页：购买声音列表不存在！");
            //设置订单明细列表
            List<OrderDetailVo> orderDetailVoList = buyTracks.stream().map(track -> {
                OrderDetailVo orderDetailVo = new OrderDetailVo();
                orderDetailVo.setItemId(track.getId());
                orderDetailVo.setItemName(track.getTrackTitle());
                orderDetailVo.setItemPrice(price);
                orderDetailVo.setItemUrl(track.getCoverUrl());
                return orderDetailVo;
            }).collect(Collectors.toList());
            orderInfoVo.setOrderDetailVoList(orderDetailVoList);
    }
}
